package com.azzcs.ttl.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @Author: wzg
 * @Date: 2021/4/12 下午5:21
 */
@Configuration
public class RabbitMQConfig {

    // 订单消息队列
    @Bean
    public Queue orderQueue(){
        HashMap<String, Object> props = new HashMap<>();
        props.put("x-message-ttl",10L*1000);
        props.put("x-dead-letter-exchange", "ex.order.dlx");
        props.put("x-dead-letter-routing-key", "rk.order.dlx");
        Queue queue = new Queue("queue.order", true, false, false, props);
        return queue;
    }

    // 订单死信队列
    @Bean
    public Queue orderDLXQueue(){
        Queue queue = new Queue("queue.order.dlx", true, false, false);
        return queue;
    }

    // 订单交换机
    @Bean
    public Exchange orderExchange(){
        return new DirectExchange("ex.order");
    }

    // 订单死信交换机
    @Bean
    public Exchange orderDLXExchange(){
        return new DirectExchange("ex.order.dlx");
    }

    // 绑定订单交换机和队列
    @Bean
    @Autowired
    public Binding orderBinding(Queue orderQueue,Exchange orderExchange){
        return BindingBuilder
                .bind(orderQueue)
                .to(orderExchange)
                .with("rk.order")
                .noargs();
    }

    // 绑定死信交换机和队列
    @Bean
    @Autowired
    public Binding orderDLXBinding(Queue orderDLXQueue,Exchange orderDLXExchange){
        return BindingBuilder
                .bind(orderDLXQueue)
                .to(orderDLXExchange)
                .with("rk.order.dlx")
                .noargs();
    }


}
